package zzs.generalframework.console.core.parameter.commonparameter.maritalstatus.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zzs.generalframework.common.core.parameter.commonparameter.maritalstatus.MaritalStatusBO;
import zzs.generalframework.common.core.parameter.commonparameter.maritalstatus.MaritalStatusInfo;
import zzs.generalframework.common.core.parameter.commonparameter.maritalstatus.MaritalStatusPO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.parameter.commonparameter.maritalstatus.dao.IMaritalStatusDao;
import zzs.generalframework.console.core.parameter.commonparameter.maritalstatus.service.IMaritalStatusService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.commonparameter.maritalstatus.service.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Service("zzs.generalframework.console.core.parameter.commonparameter.maritalstatus.service.impl.maritalStatusServiceImpl")
public class MaritalStatusServiceImpl implements IMaritalStatusService
{

	private Logger logger = LoggerFactory.getLogger(MaritalStatusServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	IMaritalStatusDao maritalStatusDao;

	@Override
	public ConsoleReturnBO saveMaritalStatus(MaritalStatusBO maritalStatusBO)
	{
		logger.debug("====保存婚姻状况信息开始====");
		logger.debug("====保存婚姻状况信息.入参:MaritalStatusBO=[{}]====", maritalStatusBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		MaritalStatusPO maritalStatusPO = new MaritalStatusPO();
		copyMaritalStatusBO2MaritalStatusPO(maritalStatusBO, maritalStatusPO);
		Boolean insertResult = maritalStatusDao.insert(maritalStatusPO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存婚姻状况信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存婚姻状况信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO saveMaritalStatus(List<MaritalStatusBO> maritalStatusBOList) throws ConsoleException
	{
		logger.info("====保存婚姻状况信息列表开始====");
		logger.debug("====保存婚姻状况信息列表.入参:MaritalStatusBOList=[{}]====", maritalStatusBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (MaritalStatusBO itemBO : maritalStatusBOList)
		{
			ConsoleReturnBO itemResult = saveMaritalStatus(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====保存婚姻状况信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存婚姻状况信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getMaritalStatus(MaritalStatusInfo maritalStatusInfo)
	{
		logger.info("====获取婚姻状况信息开始====");
		logger.debug("====获取婚姻状况信息.入参:MaritalStatusInfo=[{}]====", maritalStatusInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<MaritalStatusPO> maritalStatusPOList = maritalStatusDao.select(maritalStatusInfo);
		if (maritalStatusPOList == null || maritalStatusPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (maritalStatusPOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			MaritalStatusBO maritalStatusBO = new MaritalStatusBO();
			copyMaritalStatusPO2MaritalStatusBO(maritalStatusPOList.get(0), maritalStatusBO);
			returnConsoleReturnBO.setReturnObj(maritalStatusBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			MaritalStatusBO maritalStatusBO = new MaritalStatusBO();
			copyMaritalStatusPO2MaritalStatusBO(maritalStatusPOList.get(0), maritalStatusBO);
			returnConsoleReturnBO.setReturnObj(maritalStatusBO);
		}
		logger.debug("====获取婚姻状况信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取婚姻状况信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listMaritalStatus(MaritalStatusInfo maritalStatusInfo)
	{
		logger.info("====获取婚姻状况信息列表开始====");
		logger.debug("====获取婚姻状况信息列表.入参:MaritalStatusInfo=[{}]====", maritalStatusInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<MaritalStatusPO> maritalStatusPOList = maritalStatusDao.select(maritalStatusInfo);
		if (maritalStatusPOList == null || maritalStatusPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<MaritalStatusBO> resultList = new ArrayList<MaritalStatusBO>();
			for (MaritalStatusPO itemPO : maritalStatusPOList)
			{
				MaritalStatusBO itemBO = new MaritalStatusBO();
				copyMaritalStatusPO2MaritalStatusBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取婚姻状况信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取婚姻状况信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateMaritalStatus(MaritalStatusBO maritalStatusBO)
	{
		logger.info("====更新婚姻状况信息开始====");
		logger.debug("====更新婚姻状况信息.入参:MaritalStatusBO=[{}]====", maritalStatusBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		MaritalStatusPO maritalStatusPO = new MaritalStatusPO();
		copyMaritalStatusBO2MaritalStatusPO(maritalStatusBO, maritalStatusPO);
		Boolean updateResult = maritalStatusDao.update(maritalStatusPO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新婚姻状况信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新婚姻状况信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateMaritalStatus(List<MaritalStatusBO> maritalStatusBOList) throws ConsoleException
	{
		logger.info("====更新婚姻状况信息列表开始====");
		logger.debug("====更新婚姻状况信息列表.入参:List<MaritalStatusBO>=[{}]====", maritalStatusBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (MaritalStatusBO itemBO : maritalStatusBOList)
		{
			ConsoleReturnBO itemResult = updateMaritalStatus(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====更新婚姻状况信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新婚姻状况信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeMaritalStatus(MaritalStatusBO maritalStatusBO)
	{
		logger.info("====删除婚姻状况信息开始====");
		logger.debug("====删除婚姻状况信息.入参:MaritalStatusBO＝[{}]======", maritalStatusBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		MaritalStatusPO maritalStatusPO = new MaritalStatusPO();
		copyMaritalStatusBO2MaritalStatusPO(maritalStatusBO, maritalStatusPO);
		Boolean deleteResult = maritalStatusDao.delete(maritalStatusPO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除婚姻状况信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除婚姻状况信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeMaritalStatus(List<MaritalStatusBO> maritalStatusBOList) throws ConsoleException
	{
		logger.info("====删除婚姻状况信息列表开始====");
		logger.debug("====删除婚姻状况信息列表.入参:List<MaritalStatusBO>＝[{}]======", maritalStatusBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (MaritalStatusBO itemBO : maritalStatusBOList)
		{
			ConsoleReturnBO itemResult = removeMaritalStatus(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====删除婚姻状况信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除婚姻状况信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countMaritalStatus(MaritalStatusBO maritalStatusBO)
	{
		logger.info("====统计婚姻状况信息开始====");
		logger.debug("====统计婚姻状况信息。入参：MaritalStatusBO＝[{}]======", maritalStatusBO);
		Long returnLong = new Long(0);
		MaritalStatusPO maritalStatusPO = new MaritalStatusPO();
		copyMaritalStatusBO2MaritalStatusPO(maritalStatusBO, maritalStatusPO);
		returnLong = maritalStatusDao.count(maritalStatusPO);
		logger.debug("====统计婚姻状况信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计婚姻状况信息结束====");
		return returnLong;
	}

	private void copyMaritalStatusBO2MaritalStatusPO(MaritalStatusBO maritalStatusBO, MaritalStatusPO maritalStatusPO)
	{
		beanUtils.copyBeanNotNull(maritalStatusBO, maritalStatusPO);
		if (maritalStatusBO.getSysID() != null)
		{
			maritalStatusPO.setSeqID(maritalStatusBO.getSysID());
		}
	}

	private void copyMaritalStatusPO2MaritalStatusBO(MaritalStatusPO maritalStatusPO, MaritalStatusBO maritalStatusBO)
	{
		beanUtils.copyBeanNotNull(maritalStatusPO, maritalStatusBO);
		if (maritalStatusPO.getSeqID() != null)
		{
			maritalStatusBO.setSysID(maritalStatusPO.getSeqID());
		}
	}

}
